//index.js
const app = getApp()
var total_micro_second = '';
var total_micro_second_point = '';
var time = '';
var time1 = '';

/* 经验卡倒计时 */
function count_down(that) {
  //防止重复切换错乱
    clearTimeout(time);
  // 渲染倒计时时钟
  that.setData({
    exp_limit_time: date_format(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      exp_limit_time: ""
    });
    // timeout则跳出递归
    return;
  }
  time = setTimeout(function () {
    // 放在最后--
    total_micro_second -= 1000;
    count_down(that);
  }, 1000)
}
/* 王者币卡倒计时 */
function count_down_point(that) {
  //防止重复切换错乱
    clearTimeout(time1);
  // 渲染倒计时时钟
  that.setData({
    point_limit_time: date_format(total_micro_second_point)
  });

  if (total_micro_second_point <= 0) {
    that.setData({
      point_limit_time: ""
    });
    // timeout则跳出递归
    return;
  }
  time1 = setTimeout(function () {
    // 放在最后--
    total_micro_second_point -= 1000;
    count_down_point(that);
  }, 1000)
}
// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = fill_zero_prefix(Math.floor(second / 3600));
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));

  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  return parseInt(hr*60)+parseInt(min)+ ":" + sec;
}
// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}

Page({
  data: {
    exp_limit_time: '',
    point_limit_time:'',
    user_info:app.getUserInfo(),
    tupian: app.globalData.imgUrl,
    grad_logo: app.globalData.siteUrl,
  },
  gradPk(e){
  	let grad_num = e.currentTarget.dataset.grad_num;
        let consume_point = e.currentTarget.dataset.consume_point;
        let apiurl = 'user/is_enough_points';
        app.getdata.fetchApi(apiurl, { grad_id: grad_num})
          .then(d => {
            if (d.status == 1) {
            wx.setStorageSync('grad_id', grad_num);
            app.turnToPage('/pages/duizhan/duizhan?room_id='+grad_num+'&consume_point='+consume_point);
            console.log(grad_num)
            } else {
              app.showModal({content:d.message});
            }
          })
          .catch(e => {
            this.setData({ subtitle: '获取数据异常', loading: false })
            console.error(e)
          })
  },
  get_grad_list: function (){
      let apiurl = 'Grad/getGradList';
      app.getdata.fetchApi(apiurl, {})
      .then(d => {
        if (d.status == 1) {
          this.isReq=true;
          let user_info = app.getUserInfo();
          user_info.up_rule = d.data.up_rule;
          user_info.use_prop = d.data.use_prop;
          wx.setStorageSync('aid',d.data.activity.id);//活动id
          wx.setStorageSync('userInfo',user_info);

          this.setData({grad:d.data})
          total_micro_second=d.data.play.exp_limit_time
          total_micro_second_point = d.data.play.point_limit_time
          count_down(this);
          count_down_point(this);
        } else {
          this.setData({ hasMore: false, loading: false })
        }
      })
      .catch(e => {
        this.setData({ subtitle: '获取数据异常', loading: false })
        console.error(e)
      })
  },
  onLoad: function () {
    //防止重复切换错乱
    clearTimeout(time);
    clearTimeout(time1);

    wx.setStorageSync('room_id');
    let user_info = app.getUserInfo();
    this.setData({ ver_check_status:user_info.check_status })
    this.get_grad_list();
  },
  onShow: function (e) {
      if(this.isReq){
          this.get_grad_list();
      }
  },
  /**关闭物品详情*/
  closeDetail() {
      this.setData({openDetail: true})
  },
  showBox(e){//道具详情
    console.log(e)
    let goods_flag = e.currentTarget.dataset.goods_flag;
    let icon = e.currentTarget.dataset.icon;
    let apiurl = 'Grad/getProp';
    app.getdata.fetchApi(apiurl, { flag: goods_flag})
      .then(d => {
        if (d.status == 1) {
          this.setData({
            detail: d.data,
            openDetail: false,
            icon: icon

          })
        } else {
          this.setData({ hasMore: false, loading: false })
        }
      })
      .catch(e => {
        this.setData({ subtitle: '获取数据异常', loading: false })
        console.error(e)
      })
  },

  //使用道具
  userProp(e){
    let id = e.currentTarget.dataset.id;
    let apiurl = 'Goods/useGoods';
    app.getdata.fetchApi(apiurl, { id:id,type:2})
      .then(d => {
        if (d.status == 1) {
          app.showModal({content:d.data});
          this.setData({
            ['detail.num']: parseInt(this.data.detail.num)-1,
          })
          this.get_grad_list()
        } else {
          wx.showToast({
            title: d.message,
            icon: 'loading',
            duration: 2000
          })
        }
      })
      .catch(e => {
        this.setData({ subtitle: '获取数据异常', loading: false })
        console.error(e)
      })
  }
});


